package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-06：MethodHandles#filterArguments的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 18:32
 */
public class MethodHandlesExample06 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample06.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-06：MethodHandles#filterArguments的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType type = MethodType.methodType(int.class, int.class, int.class);
        MethodHandle mhGetLength = lookup.findVirtual(String.class, "length", MethodType.methodType(int.class));
        MethodHandle mhTarget = lookup.findStatic(Math.class, "max", type);
        MethodHandle mhResult = MethodHandles.filterArguments(mhTarget, 0, mhGetLength, mhGetLength);
        int value = (int)mhResult.invoke("Hello", "New World");

        System.out.println(value);

    }

}
